package com.slh.touchandroid.activity;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.slh.touchandroid.R;
import com.slh.touchandroid.fragment.PanelFrament;

public class SettingsPanelActivity extends BaseActivity {
	// ============================================================
	private ViewPager viewPager;
	private PagerAdapter pagerAdapter;

	public static void start(Context context) {
		Intent intent = new Intent(context, SettingsPanelActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings_panel);
		viewPager = (ViewPager) findViewById(R.id.pager);
		pagerAdapter = new PagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(pagerAdapter);
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			onBackPressed();
		}
		return super.onOptionsItemSelected(item);
	}

	private class PagerAdapter extends FragmentStatePagerAdapter {
		private ArrayList<PanelFrament> fragments;

		public PagerAdapter(FragmentManager fm) {
			super(fm);
			fragments = new ArrayList<PanelFrament>();
			fragments.add(PanelFrament.newInstance(PanelFrament.MAIN_PANEL,
					getString(R.string.main_panel)));
			fragments.add(PanelFrament.newInstance(PanelFrament.SUB_PANEL,
					getString(R.string.settings_panel)));
			fragments.add(PanelFrament.newInstance(PanelFrament.FAV_PANEL,
					getString(R.string.favorite_panel)));
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragments.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragments.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return fragments.get(position).getArguments()
					.getString(PanelFrament.KEY_TITLE);
		}

		public void transferClickEventToFragment(View view, int position) {
			fragments.get(position).onClick(view);
		}

	}

	public void onClick(View view) {
		int index = viewPager.getCurrentItem();
		pagerAdapter.transferClickEventToFragment(view, index);

	}
}
